package cn.bluethink.eguan.core.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.persistence.Id;
import javax.persistence.Table;

import org.apache.commons.lang.StringUtils;

import cn.bluethink.eguan.model.core.EgFile;
import cn.bluethink.eguan.model.core.EgOType;
import cn.bluethink.eguan.model.core.EgObject;
import cn.bluethink.eguan.model.core.EgResult;

/**
 * 驿馆提交的结果实体
 * 
 * @author 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.3.0
 * @brief 驿馆+产品
 * @note 修订历史： 1.wanglei于2019/01/30设计并构建初始版本v0.3.0
 */
@Table(name = "eg_result")
public class EgResultEntity implements Serializable {

    private static final long serialVersionUID = -1631422419822902651L;

    /**
     * 主键
     */
    @Id
    private Long id;

    /**
     * 结果描述
     */
    private String des;

    /**
     * 文件列表
     */
    private String files;

    /**
     * 此结果所针对的对象类型
     */
    private Integer otype;

    /**
     * 此结果所针对的对象
     */
    private Long oid;

    /**
     * 结果提交者
     */
    private Long uid;

    /**
     * 结果提交时间
     */
    private Date ctime;

    public EgResultEntity() {

    }

    /**
     * 创建实体
     * 
     * @param result
     */
    public EgResultEntity(EgResult result) throws Exception {
        this.id = result.getId();
        this.des = result.getDes() == null ? "" : result.getDes();

        List<EgFile> fileList = result.getFileList();
        if (fileList != null && fileList.size() > 0) {
            List<String> fileIds = fileList.stream().map(f -> String.valueOf(f.getId())).collect(Collectors.toList());
            this.files = StringUtils.join(fileIds, ',');
        }

        this.otype = result.getObj() == null ? null : result.getObj().getType().getOtid();
        this.oid = result.getObj() == null ? null : result.getObj().getId();
        this.uid = result.getUser() == null ? null : result.getUser().getId();
        this.ctime = new Date();
    }

    /**
     * 转为业务模型
     * 
     * @return
     */
    public EgResult creatEgResult() throws Exception {
        EgResult result = new EgResult(id, null, new EgOType());
        result.setDes(des);

        List<EgFile> fileList = new ArrayList<>();
        if (StringUtils.isNotBlank(files)) {
            String[] fileIds = files.split(",");
            for (String fileId : fileIds) {
                fileList.add(new EgFile(Long.valueOf(fileId), null, null));
            }
        }
        result.setFileList(fileList);

        if (oid != null && otype != null) {
            result.setObj(new EgObject(oid, null, new EgOType(otype, null)));
        } else {
            result.setObj(null);
        }

        if (uid != null && uid != 0) {
            result.setUser(new EgObject(uid, null, new EgOType(EgOType.USER, null)));
        } else {
            result.setUser(null);
        }
        result.setCtime(ctime);
        return result;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }

    public String getFiles() {
        return files;
    }

    public void setFiles(String files) {
        this.files = files;
    }

    public Integer getOtype() {
        return otype;
    }

    public void setOtype(Integer otype) {
        this.otype = otype;
    }

    public Long getOid() {
        return oid;
    }

    public void setOid(Long oid) {
        this.oid = oid;
    }

    public Long getUid() {
        return uid;
    }

    public void setUid(Long uid) {
        this.uid = uid;
    }

    public Date getCtime() {
        return ctime;
    }

    public void setCtime(Date ctime) {
        this.ctime = ctime;
    }

}
